home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / compuserve-file-archive / 09 Application Software / CONVER.DOC < prev    next >
Text File  |  2019-04-13  |  10KB  |  242 lines

  1.     MACHINE CODE PROGRAMMING EXAMPLE                                 page 1
  2.  
  3.  
  4.     Have you ever looked at a doc file and discovered it was an ASCII file
  5.     instead of PETASCII.  "NEW UTILITY" has a word-processor in it that
  6.     would convert it to a PETASCII, but some files are too long to fit, so
  7.     here is a program to convert them.
  8.  
  9.     First, here is what the program had to do:
  10.  
  11.          (1) Discard any linefeeds.  This is a hex $0a or a decimal 10
  12.          code.
  13.  
  14.          (2) Convert any values beginning at $41 (decimal 65) and ending
  15.          with $5a (decimal 90) to corresponding values in the range $c1 to
  16.          $da.  This simply involved setting the high bit of the orginal
  17.          value.  These values represent uppercase characters.
  18.  
  19.          (3) Convert values in the $61 to $7a range to the corresponding
  20.          values in the $41 to $5a range.  This is accomplished by
  21.          subtracting $20 from the orginal value.These values represent the
  22.          lower case letters.
  23.  
  24.     To accomplish these objectives, the program combines a combination of
  25.     BASIC and machine language.  BASIC is used to open the file and check
  26.     for disk errors and machine language to do the converting and write it
  27.     back to disk.  The file is converted as it is read rather than have 2
  28.     files open to the disk (it can be done if you want) The converted file
  29.     is stored in a buffer beginning at $2000 (decimal 8192).  When the
  30.     initial file has been converted, the program returns to BASIC to check
  31.     for disk errors, close the file, open the write file, check for a good
  32.     open, and then sys back to the machine language routine to write the
  33.     file to disk.  A sequential file can never contain a hex $00, so that
  34.     is used to mark the end of the file in the buffer.  When that marker is
  35.     reached, the program returns to BASIC to check for errors and close the
  36.     file.  It's all straigt-forward and a beginner may be able to follow
  37.     the program well enough to get some understanding of machine language
  38.     programming.
  39.  
  40.     Beginning at line 10, here is an explanation of the listing:
  41.  
  42.          Line 10 thru 13 initializes a pointer located in zero page to the
  43.          address for the beginning of the buffer.
  44.  
  45.          Line 14 and 15 set up file #8 as our input file.
  46.  
  47.          Line 16 is the beginning of the main loop of this module.  It gets
  48.          a byte from the disk drive.
  49.  
  50.          Line 17 and 18 handle the line feeds contained in the ASCII file.
  51.          If these are left in the converted file then the printout will be
  52.          double spaced.  A "CMP" op-code can give 3 possible results which
  53.          are:
  54.               (1) BCC - less-than (the carry is clear)
  55.               (2) BEQ - equal to (the zero flag is set)
  56.               (3) BCS - equal-to or greater-than (the carry-flag is set).
  57.  
  58.          Lines 19 through 22 will trap all the codes in the $41 to $5a
  59.          range.  In line 20, the branch will occur if the value is
  60.          LESS-THAN a $41.  In line 22 the branch will occur if the value is
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.          MACHINE CODE PROGRAMMING EXAMPLE                            page 2
  68.  
  69.  
  70.          GREATER-THAN a hex $5a.  The comparison is actually made to a hex
  71.          $5b because the carry is set if the values are equal.
  72.  
  73.          Line 23 sets the high bit of the orginal value and line 24 will
  74.          always branch because no value can be zero with the high bit set.
  75.  
  76.          Line 25 through 28 traps the values in the range of $61 to $7a.
  77.  
  78.          Lines 29 through 30 convert the values by subtracting a hex $20
  79.          from the orginal value.  When the SBC opcode is used, it must be
  80.          preceded with the SEC opcode unless the status of the carry flag
  81.          is already known.
  82.  
  83.          Lines 30 through 34 handle storing the converted value to the
  84.          buffer.  The Y Register is set to zero in line 10 and it has not
  85.          been changed to this point.  The value is actually stored at the
  86.          address pointed to at aux ($8b) PLUS the value of the Y register.
  87.          So the first time through the loop, aux contains the address $2000
  88.          in standard lo-byte - hi-byte format ($00 $20) and Y is 0, so the
  89.          first byte is stored at $2000.  The second time around the loop,
  90.          the value is stored at $2001 because y is now 1.  Y will be
  91.          incremented up to 255 ($FF) and at that point the value will be
  92.          stored at $20FF.  When Y is incremented the new value will be 0
  93.          and this sets the zero flag tested by the BNE op-code, and since
  94.          it is "NOT EQUAL TO ZERO" then this branch will not occur and then
  95.          aux+1 is incremented.  Aux will now contain a pointer to the
  96.          address $2100.  This is how the converted values are stored in the
  97.          buffer and this process will continue as long as the main loop
  98.          continues.
  99.  
  100.          Lines 35 to 38 check for the EOF and terminate the loop when it is
  101.          detected.  The EOF is indicated by the sixth bit of the status
  102.          byte being set.  When you AND the status byte with $40 (binary
  103.          0100 0000) the only two possible results are 0 and $40.  Our
  104.          branch here occurs if the result is zero (in other words, the
  105.          sixth bit was NOT set).
  106.  
  107.          Lines 38 and 39 put the eof marker in the buffer.
  108.  
  109.          Line 40 restores the input and output devices to normal (input
  110.          keyboard - output screen).
  111.  
  112.          Line 41 will return us to BASIC.  The file has now been read from
  113.          the disk, converting it to PETASCII, and storing it to the
  114.          temporary buffer.
  115.  
  116.          Line 42 is the beginning of the ML code for use when BASIC SYS's
  117.          back to write the converted file to disk.  Line 42 through 45
  118.          initialize the zero page pointer to point to the address of $2000
  119.          (beginning of the temporary buffer).
  120.  
  121.          Line 46 and 47 set up the disk file 8 as the output channel.
  122.  
  123.          Line 48 initialized the Y Register to the value of zero.
  124.  
  125.          Line 49 is the beginning of the main loop for the write module.
  126.          It gets a byte from the temporary buffer.
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.          MACHINE CODE PROGRAMMING EXAMPLE                            page 3
  134.  
  135.  
  136.          Line 50 tests for the EOF marker and branches if detected.
  137.  
  138.          Line 51 outputs the byte to the output channel (disk drive in this
  139.          case).
  140.  
  141.          Lines 52 to 55 take care of managing the Y Register and the zero
  142.          page pointer.  This works the same as in lines 32 through 34.
  143.  
  144.          Line 56 restores the normal input and output channels and line 57
  145.          returns to BASIC.  We only get here by detecting the EOF marker
  146.          back in line 50.
  147.  
  148.     The finished code is located in the cassette buffer at decimal 828.
  149.     The object code is only 98 bytes long.  The cassette buffer can hold up
  150.     to 192 bytes.  This code is special in that it is "relocatable", ie.,
  151.     it can run at any address with out being modified.
  152.  
  153.     Once the machine code was written and tested, then it was converted it
  154.     to data statements and added it to the BASIC program.  This was done
  155.     with a program called "make data lines" and then the data file
  156.     generated was appended to the BASIC file with Tiny Aid.  The completed
  157.     file was re-numbered with Tiny Aid to give it a finishing touch.
  158.  
  159.     A side by side listing of the Machine Language portion follows.  Hope
  160.     you enjoy this and let me know if you have any questions.
  161.  
  162.  
  163.  
  164.                              <<<<<...GAIL...>>>>>
  165.  
  166.  
  167.  
  168.  
  169.                         LISTING FROM MERLIN ASSEMBLER
  170.  
  171.                              Program by Gail Cox
  172.  
  173.                     1    AUX      =     $8B
  174.                     2    READST   =     $FFB7
  175.                     3    CHKIN    =     $FFC6
  176.                     4    CHKOUT   =     $FFC9
  177.                     5    CLRCHN   =     $FFCC
  178.                     6    CHROUT   =     $FFD2
  179.                     7    GETIN    =     $FFE4
  180.                     8    *
  181.                     9             ORG   828
  182.     033C: A0 00     10   START    LDY   #$00
  183.     033E: 84 8B     11            STY   AUX
  184.     0340: A9 20     12            LDA   #$20
  185.     0342: 85 8C     13            STA   AUX+1
  186.     0344: A2 08     14            LDX   #$08
  187.     0346: 20 C6 FF  15            JSR   CHKIN
  188.     0349: 20 E4 FF  16   MAIN     JSR   GETIN
  189.     034C: C9 0A     17            CMP   #$0A       ;no linefeed
  190.     034E: F0 F9     18            BEQ   MAIN
  191.     0350: C9 41     19            CMP   #'a'       ;$41
  192.     0352: 90 13     20            BCC   OK
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.     MACHINE CODE PROGRAMMING EXAMPLE                                 page 4
  200.  
  201.  
  202.     0354: C9 5B     21            CMP   #'z'+1     ;$7b
  203.     0356: B0 04     22            BCS   CKMOR
  204.     0358: 09 80     23            ORA   #$80
  205.     035A: D0 0B     24            BNE   OK
  206.     035C: C9 61     25   CKMOR    CMP   #'A'       ;$61
  207.     035E: 90 07     26            BCC   OK
  208.     0360: C9 7B     27            CMP   #'Z'+1     ;$7b
  209.     0362: B0 03     28            BCS   OK
  210.     0364: 38        29            SEC
  211.     0365: E9 20     30            SBC   #$20
  212.     0367: 91 8B     31   OK       STA   (AUX),Y
  213.     0369: C8        32            INY
  214.     036A: D0 02     33            BNE   CKST
  215.     036C: E6 8C     34            INC   AUX+1
  216.     036E: 20 B7 FF  35   CKST     JSR   READST
  217.     0371: 29 40     36            AND   #$40
  218.     0373: F0 D4     37            BEQ   MAIN
  219.     0375: A9 00     38            LDA   #$00
  220.     0377: 91 8B     39            STA   (AUX),Y
  221.     0379: 20 CC FF  40            JSR   CLRCHN
  222.     037C: 60        41            RTS
  223.     037D: A9 00     42   WRITE    LDA   #$00
  224.     037F: 85 8B     43            STA   AUX
  225.     0381: A9 20     44            LDA   #$20
  226.     0383: 85 8C     45            STA   AUX+1
  227.     0385: A2 08     46            LDX   #$08
  228.     0387: 20 C9 FF  47            JSR   CHKOUT
  229.     038A: A0 00     48            LDY   #$00
  230.     038C: B1 8B     49   WL       LDA   (AUX),Y
  231.     038E: F0 0A     50            BEQ   FINISH
  232.     0390: 20 D2 FF  51            JSR   CHROUT
  233.     0393: C8        52            INY
  234.     0394: D0 F6     53            BNE   WL
  235.     0396: E6 8C     54            INC   AUX+1
  236.     0398: D0 F2     55            BNE   WL
  237.     039A: 20 CC FF  56   FINISH   JSR   CLRCHN
  238.     039D: 60        57            RTS
  239.  
  240.  
  241.     --End assembly, 98 bytes, Errors: 0
  242.